package com.zenni.core;

import com.zenni.utils.DriverUtils;
import com.zenni.utils.LogUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;

import java.util.logging.Logger;

/**
 * Created by douglasj on 3/1/16.
 */
public class HomePageFunction {
    private WebDriver driver = null;
    private DriverUtils driverUtils = new DriverUtils();
    //public static LogUtils logUtils = new LogUtils();
    Logger logger = Logger.getLogger(this.getClass().getName());
    public HomePageFunction(WebDriver driver) throws Exception{
        this.driver = driver;
    }
    public void closethepopwindow() throws Exception{
        if (driverUtils.isElementExistById(driver, "popup-subcription-backgrounds-container-124d70ff-1403-4327-b91f-cad5f2d24e8a")){
            driver.findElement(By.id("popup-subcription-closes-link-124d70ff-1403-4327-b91f-cad5f2d24e8a")).click();
        }
    }
    public void loadAndVerfyInfoPage(String home_page_url) throws InterruptedException{
        logger.info("We are accessing "+home_page_url);
        driver.get(home_page_url);
    }
    public void expandWomenDropDownMenu() throws Exception{
        logger.info("Click the > button next to women");
        if(driver.findElement(By.xpath(HomePageVariables.hamburger_menu_button_xpath)).isDisplayed()){
            driver.findElement(By.xpath(HomePageVariables.hamburger_menu_button_xpath)).click();
            driverUtils.waitTillElementExistsByXpath(driver,HomePageVariables.women_expand_icon_xpath_mobile,3000);
            driver.findElement(By.xpath(HomePageVariables.women_expand_icon_xpath_mobile)).click();
        }else {
            driver.findElement(By.xpath(HomePageVariables.women_expand_icon_xpath)).click();
        }
    }
    public void expandMenDropDownMenu() throws Exception{
        logger.info("Click the > button next to men");
        if(driver.findElement(By.xpath(HomePageVariables.hamburger_menu_button_xpath)).isDisplayed()){
            driver.findElement(By.xpath(HomePageVariables.hamburger_menu_button_xpath)).click();
            driverUtils.waitTillElementExistsByXpath(driver,HomePageVariables.men_expand_icon_xpath_mobile,3000);
            driver.findElement(By.xpath(HomePageVariables.men_expand_icon_xpath_mobile)).click();
        }else {
            driver.findElement(By.xpath(HomePageVariables.men_expand_icon_xpath)).click();
        }
    }
    public void expandMyAccountDropDownMenu() throws Exception{
        logger.info("Click the > button next to myaccount");
        if(driver.findElement(By.xpath(HomePageVariables.hamburger_menu_button_xpath)).isDisplayed()){
            driver.findElement(By.xpath(HomePageVariables.hamburger_menu_button_xpath)).click();
            driverUtils.waitTillElementExistsByXpath(driver,HomePageVariables.my_account_expand_icon_xpath_mobile,3000);
            driver.findElement(By.xpath(HomePageVariables.my_account_expand_icon_xpath_mobile)).click();
        }else {
            driver.findElement(By.xpath(HomePageVariables.my_account_expand_icon_xpath)).click();
        }
    }
    public void clickKidsInHeader() throws Exception{
        logger.info("Click kid in the header");
        if(driver.findElement(By.xpath(HomePageVariables.hamburger_menu_button_xpath)).isDisplayed()){
            driver.findElement(By.xpath(HomePageVariables.hamburger_menu_button_xpath)).click();
            driverUtils.waitTillElementExistsByXpath(driver,HomePageVariables.kids_link_xpath_mobile,3000);
            driver.findElement(By.xpath(HomePageVariables.kids_link_xpath_mobile)).click();
        }else {
            driver.findElement(By.xpath(HomePageVariables.kids_link_xpath)).click();
        }
    }
    public void clickCartInHeader() throws Exception{
        logger.info("Click cart button in the header");
        if(driver.findElement(By.xpath(HomePageVariables.hamburger_menu_button_xpath)).isDisplayed()) {
            driver.findElement(By.xpath(HomePageVariables.hamburger_menu_button_xpath)).click();
            driverUtils.waitTillElementExistsByXpath(driver,HomePageVariables.cart_link_xpath_mobile,3000);
            driver.findElement(By.xpath(HomePageVariables.cart_link_xpath_mobile)).click();
        }else {
            driver.findElement(By.xpath(HomePageVariables.cart_link_xpath)).click();
        }
    }
    public void enterSearchContents(String contents) throws Exception{
        logger.info("We will typing "+contents+" into the search box");
        if(driver.findElement(By.xpath(HomePageVariables.hamburger_menu_button_xpath)).isDisplayed()) {
            driver.findElement(By.xpath(HomePageVariables.hamburger_menu_button_xpath)).click();
            driverUtils.waitTillElementExistsByXpath(driver,HomePageVariables.search_box_block_mobile,3000);
            driver.findElement(By.xpath(HomePageVariables.search_box_block_mobile)).click();
            driver.findElement(By.xpath(HomePageVariables.search_box_xpath_mobile)).sendKeys(contents);
            driver.findElement(By.xpath(HomePageVariables.search_button_xpath_mobile)).click();
        }else {
            driver.findElement(By.xpath(HomePageVariables.search_box_xpath)).sendKeys(contents);
            driver.findElement(By.xpath(HomePageVariables.search_button_xpath)).click();
        }
    }
    public void verifySearchPage(String header) throws Exception {
        logger.info("Verify if we are accessing the search page");
        String currentpageheader = driver.findElement(By.xpath(HomePageVariables.search_result_page_header_xpath)).getText();
        Assert.assertEquals(currentpageheader, header);
    }
    public void clickLoginOrRegisterButton() throws Exception{
        logger.info("Click the login/register link");
        if(driver.findElement(By.xpath(HomePageVariables.hamburger_menu_button_xpath)).isDisplayed()) {
            driver.findElement(By.xpath(HomePageVariables.hamburger_menu_button_xpath)).click();
            driverUtils.waitTillElementExistsByXpath(driver,HomePageVariables.login_or_register_link_xpath_mobile,3000);
            driver.findElement(By.xpath(HomePageVariables.login_or_register_link_xpath_mobile)).click();
        }else {
            driver.findElement(By.xpath(HomePageVariables.login_or_register_link_xpath)).click();
        }
    }
    public void goToAccountView() throws Exception{
        logger.info("Click account view");
        if(driver.findElement(By.xpath(HomePageVariables.hamburger_menu_button_xpath)).isDisplayed()) {
            //driver.findElement(By.xpath(HomePageVariables.hamburger_menu_button_xpath)).click();
            driverUtils.waitTillElementExistsByXpath(driver,HomePageVariables.account_view_mobile_xpath,3000);
            driver.findElement(By.xpath(HomePageVariables.account_view_mobile_xpath)).click();
        }else {
            driver.findElement(By.xpath(HomePageVariables.account_view_xpath)).click();
        }
    }
    public void goToMyFavorite() throws Exception{
        if(driver.findElement(By.xpath(HomePageVariables.hamburger_menu_button_xpath)).isDisplayed()) {
            driverUtils.waitTillElementExistsByXpath(driver,HomePageVariables.my_favorite_mobile_xpath,3000);
            driver.findElement(By.xpath(HomePageVariables.my_favorite_mobile_xpath)).click();
        }else {
            driver.findElement(By.xpath(HomePageVariables.my_favorite_xpath)).click();
        }
    }
    public void goToMyOrders() throws Exception{
        if(driver.findElement(By.xpath(HomePageVariables.hamburger_menu_button_xpath)).isDisplayed()) {
            driverUtils.waitTillElementExistsByXpath(driver,HomePageVariables.my_order_mobile_xpath,3000);
            driver.findElement(By.xpath(HomePageVariables.my_order_mobile_xpath)).click();
        }else {
            driver.findElement(By.xpath(HomePageVariables.my_order_xpath)).click();
        }
    }
    public void goToGiftCard() throws Exception{
        if(driver.findElement(By.xpath(HomePageVariables.hamburger_menu_button_xpath)).isDisplayed()) {
            driverUtils.waitTillElementExistsByXpath(driver,HomePageVariables.gift_card_balance_mobile_xpath,3000);
            driver.findElement(By.xpath(HomePageVariables.gift_card_balance_mobile_xpath)).click();
        }else {
            driver.findElement(By.xpath(HomePageVariables.gift_card_balance_xpath)).click();
        }
    }
    public void selectOneFrameFromBestSellers() throws Exception{
        logger.info("Click the first frame of best sellers");
        driverUtils.waitTillElementExistsByXpath(driver,HomePageVariables.best_seller_frame_xpath,3000);
        if(driver.findElement(By.xpath(HomePageVariables.hamburger_menu_button_xpath)).isDisplayed()) {
            driver.findElement(By.xpath(HomePageVariables.best_seller_frame_xpath)).click();
        }else {
            /*Actions actions = new Actions(driver);
            actions.moveToElement(driver.findElement(By.xpath(HomePageVariables.best_seller_frame_xpath))).click().build().perform();*/
            String visiblejs = "document.getElementsByClassName('ui-best-sellers margin-top50 margin-bottom50')[0].getElementsByClassName('col-xs-6 ui-order')[0].getElementsByTagName('button')[0].style.visibility='visible'";
            JavascriptExecutor js = (JavascriptExecutor)driver;
            js.executeScript("document.getElementsByClassName('ui-best-sellers margin-top50 margin-bottom50')[0].getElementsByClassName('col-xs-6 ui-order')[0].getElementsByTagName('button')[0].click()");
            //driver.findElement(By.xpath(HomePageVariables.best_seller_frame_xpath)).click();
        }
        Thread thread = new Thread();
        thread.sleep(3000);
    }
    public void selectOneFrameFromNewArrivals() throws Exception {
        logger.info("Click the first frame of new arrivals");
        driverUtils.waitTillElementExistsByXpath(driver, HomePageVariables.new_arrival_frame_xpath, 3000);
        //driver.findElement(By.xpath(HomePageVariables.new_arrival_frame_xpath)).click();
        if (driver.findElement(By.xpath(HomePageVariables.hamburger_menu_button_xpath)).isDisplayed()) {
            driver.findElement(By.xpath(HomePageVariables.new_arrival_frame_xpath)).click();
        } else {
            /*Actions actions = new Actions(driver);
            actions.moveToElement(driver.findElement(By.xpath(HomePageVariables.new_arrival_frame_xpath))).click().perform();*/
            String clicknewarrivals = "document.getElementsByClassName('ui-new-arrive margin-top50 margin-bottom50')[0].getElementsByClassName('col-xs-6 ui-order')[0].getElementsByTagName('button')[0].click()";
            JavascriptExecutor js = (JavascriptExecutor)driver;
            js.executeScript(clicknewarrivals);
        }
        Thread thread = new Thread();
        thread.sleep(3000);
    }
}
